package com.ddxcloud.movie.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddxcloud.common.utils.Result;
import com.ddxcloud.movie.dto.CreateOrderDTO;
import com.ddxcloud.movie.dto.PageDTO;
import com.ddxcloud.movie.entity.Order;
import com.ddxcloud.movie.service.OrderService;
import com.ddxcloud.movie.dto.OrderDetailsDTO;
import com.ddxcloud.movie.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/order")
@Api(tags = "订单")
public class OrderController {

    @Resource
    OrderService orderService;

    /**
     * 提交订单
     * @param dto
     * @param bindingResult
     * @return
     */
    @Operation(summary = "提交订单")
    @PostMapping("/addOrder")
    public Result<Object> createOrder(@Validated @RequestBody CreateOrderDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String errorMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            return Result.fail(errorMsg);
        }
        Order order = orderService.createOrder(dto);
        if (ObjectUtil.isNotEmpty(order)) {
            return Result.success(order);
        }
        return Result.fail();
    }

    /**
     * 订单异步通知
     * @param dto
     * @return
     */
    @Operation(summary = "订单异步通知", hidden = true)
    @PostMapping("/notify")
    public String notify(@RequestBody OrderDetailsDTO dto) {
        return orderService.notify(dto);
    }

    /**
     * 订单列表
     * @param dto
     * @return
     */
    @Operation(summary = "订单列表")
    @GetMapping("/getOrderList")
    public Result<IPage<OrderVO>> getOrderList(PageDTO dto) {
        return Result.success(orderService.getOrderList(dto));
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Operation(summary = "订单详情")
    @GetMapping("/getOrderInfo")
    public Result<OrderVO> getOrderInfo(Long id) {
        return Result.success(orderService.getOrderInfo(id));
    }
}
